package com.lagou.edu.controller;

import com.lagou.edu.service.ConsumerComponent;
import com.lagou.edu.service.DemoService;
import com.lagou.edu.service.TestMethodService;
import com.lagou.edu.utils.IpRecordUtil;
import com.lagou.edu.utils.IpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.*;

@RestController
@RequestMapping("/")
public class DubboClassworkConsumerController {

    @Autowired
    private ConsumerComponent consumerComponent;

    @Autowired
    private TestMethodService testMethodService;

    @GetMapping
    public String demo(HttpServletRequest request) {
        IpRecordUtil.setIp(IpUtil.getIpAddr(request));
        return "success from " + consumerComponent.getDemoIp();
    }

    @GetMapping("start")
    public String startRequestMethod() throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(100, 10000,
                5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(5000));
        while (true) {
            TimeUnit.MILLISECONDS.sleep(20);
            executor.execute(() -> {
                try {
                    String s = consumerComponent.randomMethod();
//                    System.out.println("请求结果：" + s);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
//        return "success";
    }
}
